<?php
	global $_GPC, $_W;
		load()->func("file");  
		load()->func('communication'); 
		load()->func('logging');
		$uniacid = $_W["uniacid"];
		$card_id = $_GPC['id'];
        $page= 'pages/card/bdindex/bdindex?card_id='.$card_id;
	    $card = pdo_fetch("select * from " . tablename("bbyscm_card") . " where id=:id and uniacid=:uniacid limit 1", array(":id" => $card_id, ":uniacid" => $uniacid));
		if (empty($card['bp_qr'])){
        $setc = pdo_fetch("SELECT `appid`,`appsecret` FROM " . tablename("bbyscm_card_sysset") . " WHERE `uniacid`= :uniacid  limit 1 ", array(":uniacid" => $uniacid));
		$appid = $setc['appid'];
		$secret = $setc['appsecret'];
		$tokenUrl = 'https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid=' . $appid . '&secret=' . $secret . '';
		$tk = file_get_contents($tokenUrl);
		$arr = (array)json_decode($tk, true);
		$tke = $arr['access_token'];
		$width=430;
        $post_data='{"path":"'.$page.'","width":'.$width.'}';
        $url = 'https://api.weixin.qq.com/wxa/getwxacode?access_token='. $tke .'';
		$res = ihttp_post($url, $post_data);
	    $result = $res['content'];
	  $pathurl =  "../attachment/images/" . $uniacid . '/' . date("Y/m/d");
	  mkdirs($pathurl);
	  $acodeget_file = $pathurl . '/qr-img-' . rand() . '.jpg';
	  
	  if (file_write($acodeget_file,$result)) {
						if (!empty($_W['setting']['remote']['type'])) {
                        $path2 = str_replace("../attachment/", "", $acodeget_file);
					    file_remote_upload($path2);
					//	return $this->result(0, '上传成功', $_W['attachurl_remote'].$path2);
					} else {
						 $path2 = str_replace("../attachment/", "", $acodeget_file);
						file_upload($path2);
				//	return $this->result(0, '上传成功', $_W['attachurl_local'].$path2);
				}
	  
		  	$data = array(
			"bp_qr" => $path2,
			"createtime" => time(),	
			);
		pdo_update("bbyscm_card", $data, array("id" => $card_id));
		$acode = tomedia($path2);
		}
		}else{
		$bp_qr = $card['bp_qr'];
		$acode = tomedia($bp_qr);
	}
   echo json_encode(array('status' => true, 'imgUrl' => $acode));
   return NULL;

?>